package qc.module.platform.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import qc.common.core.exception.QCPromptException;
import qc.common.core.pair.KeyValuePairDto;
import qc.module.platform.dto.dict.DictCategoryDto;
import qc.module.platform.dto.dict.DictCategoryQueryConditionDto;
import qc.module.platform.dto.dict.DictItemDto;
import qc.module.platform.dto.dict.DictItemQueryConditionDto;
import qc.module.platform.service.DictService;

import java.util.List;

/**
 * DictController
 *
 * @author QuCheng Tech
 * @since 2024/3/9
 */
@RestController
@RequestMapping("/dict")
public class DictController {
    private DictService service;

    @Autowired
    public void setService(DictService service) {
        this.service = service;
    }

    //是否为逻辑删除，为true表示逻辑删除--修改记录标记，为false表示物理删除--从数据库表中删除记录
    private static final boolean isLogicDelete = false;
    
    /**
     * 查询字典分类
     * 
     * @param conditionDto 查询条件对象
     * @return List<DictCategoryDto> 字典分类集合
     * @author Qucheng Tech
     * @since 2024/4/17
     */
    @RequestMapping(value = "/queryctg",method = {RequestMethod.GET,RequestMethod.POST})
    public List<DictCategoryDto> queryCategory(@RequestBody DictCategoryQueryConditionDto conditionDto){
        return service.queryCategory(conditionDto);
    }

    /**
     * 获取指定字典分类
     * 
     * @param id 字典分类编码
     * @return DictCategoryDto
     * @author Qucheng Tech
     * @since 2024/4/17
     */
    @GetMapping("/getctg")
    public DictCategoryDto getCategoryByParam(@RequestParam String id) throws QCPromptException {
        return service.getCategory(id);
    }

    /**
     * 获取指定字典分类
     *
     * @param id 字典分类编码
     * @return DictCategoryDto
     * @author Qucheng Tech
     * @since 2024/4/17
     */
    @GetMapping("/getctg/{id}")
    public DictCategoryDto getCategoryByPath(@PathVariable String id) throws QCPromptException {
        return service.getCategory(id);
    }

    /**
     * 新增字典分类
     * 
     * @param dto
     * @return 成功返回null，失败返回错误信息
     * @author Qucheng Tech
     * @since 2024/4/17
     */
    @RequestMapping(value = "/addctg",method = {RequestMethod.GET,RequestMethod.POST})
    public String addCategory(@RequestBody DictCategoryDto dto){
        return service.addCategory(dto);
    }

    /**
     * 修改字典分类
     *
     * @param dto
     * @return 成功返回null，失败返回错误信息
     * @author Qucheng Tech
     * @since 2024/4/18
     */
    @RequestMapping(value = "updatectg",method = {RequestMethod.POST,RequestMethod.PUT})
    public String updateCategory(@RequestBody DictCategoryDto dto){
        return service.updateCategory(dto);
    }

    /**
     * 删除字典分类
     * 
     * @param id 字典分类编码
     * @return 成功返回null，失败返回错误信息
     * @author Qucheng Tech
     * @since 2024/4/18
     */
    @DeleteMapping("/deletectg")
    public String deleteCategoryByParam(@RequestParam String id){
        return  service.deleteCategory(id);
    }

    /**
     * 删除字典分类
     *
     * @param id 字典分类编码
     * @return 成功返回null，失败返回错误信息
     * @author Qucheng Tech
     * @since 2024/4/18
     */
    @DeleteMapping("/deletectg/{id}")
    public String deleteCategoryByPath(@PathVariable String id){
        return service.deleteCategory(id);
    }

    /**
     * 查询字典项
     * 查询指定字典分类编码字典项集合
     * 
     * @param conditionDto
     * @return List<DictItemDto>
     * @author Qucheng Tech
     * @since 2024/4/18
     */
    @RequestMapping(value = "queryitems",method = {RequestMethod.GET,RequestMethod.POST})
    public List<DictItemDto> queryItem(@RequestBody DictItemQueryConditionDto conditionDto) throws QCPromptException {
        return service.queryItem(conditionDto);
    }


    /**
     * 获取指定字典项
     *
     * @param id 字典项编码
     * @return DictItemDto
     * @author Qucheng Tech
     * @since 2024/4/18
     */
    @GetMapping("/getitem")
    public DictItemDto getItemByParam(@RequestParam String id) throws QCPromptException {
        return service.getItem(id);
    }

    /**
     * 获取指定字典项
     *
     * @param id 字典项编码
     * @return DictItemDto
     * @author Qucheng Tech
     * @since 2024/4/18
     */
    @GetMapping("/getitem/{id}")
    public DictItemDto getItemByPath(@PathVariable String id) throws QCPromptException {
        return service.getItem(id);
    }

    /**
     * 新增字典项
     *
     * @param dto
     * @return 成功返回null，失败返回错误信息
     * @author Qucheng Tech
     * @since 2024/4/18
     */
    @RequestMapping(value = "/additem",method = {RequestMethod.GET,RequestMethod.POST})
    public String addItem(@RequestBody DictItemDto dto){
        return service.addItem(dto);
    }

    /**
     * 修改字典项
     *
     * @param dto
     * @return 成功返回null，失败返回错误信息
     * @author Qucheng Tech
     * @since 2024/4/18
     */
    @RequestMapping(value = "updateitem",method = {RequestMethod.POST,RequestMethod.PUT})
    public String updateItem(@RequestBody DictItemDto dto){
        return service.updateItem(dto);
    }

    /**
     * 删除字典项
     *
     * @param id 字典项ID
     * @return 成功返回null，失败返回错误信息
     * @author Qucheng Tech
     * @since 2024/4/18
     */
    @DeleteMapping("/deleteitem")
    public String deleteItemByParam(@RequestParam String id){
        return service.deleteItem(id,isLogicDelete);
    }
    
    /**
     * 删除字典项
     *
     * @param id 字典项ID
     * @return 成功返回null，失败返回错误信息
     * @author Qucheng Tech
     * @since 2024/4/18
     */
    @DeleteMapping("/deleteitem/{id}")
    public String deleteItemByPath(@PathVariable String id){
        return service.deleteItem(id,isLogicDelete);
    }
    
    
    @GetMapping("/values")
    public List<KeyValuePairDto> getCategoryItemValuesByParam(@RequestParam(name = "id", required = true) String id) throws QCPromptException {
        return service.getCategoryValidValues(id);
    }

    @GetMapping("/values/{id}")
    public List<KeyValuePairDto> getCategoryItemValuesByPath(@PathVariable(name = "id", required = true) String id) throws QCPromptException {
        return service.getCategoryValidValues(id);
    }

    @GetMapping("/allvalues")
    public List<KeyValuePairDto> getCategoryItemAllValuesByParam(@RequestParam(name = "id", required = true) String id) throws QCPromptException {
        return service.getCategoryAllValues(id);
    }

    @GetMapping("/allvalues/{id}")
    public List<KeyValuePairDto> getCategoryItemAllValuesByPath(@PathVariable(name = "id", required = true) String id) throws QCPromptException {
        return service.getCategoryAllValues(id);
    }
}
